﻿using System;
using Business.Common.Exception;
using Business.Common.QueryModel;
using Business.Common.Toolkit;
using Business.DataAccess.Repository.Warehouse;
using Business.Domain.Warehouse;

namespace Business.Component
{
    public class SettingManager
    {
        public static Setting GetSetting(int warehouseId, string settingCode)
        {
            var query = new Query();
            query.Criteria.Add(new Criterion("WarehouseId", CriteriaOperator.Equal, warehouseId));
            query.Criteria.Add(new Criterion("SettingCode", CriteriaOperator.Equal, settingCode));

            var settingRepository = new SettingRepository();
            return settingRepository.GetByQuery(query);
        }

        public static Setting CreateSetting(int warehouseId, string settingCode, string valueType, string settingValue,
                                            string remark, int userId)
        {
            var setting = new Setting
                              {
                                  WarehouseId = warehouseId,
                                  SettingCode = settingCode,
                                  SettingValue = settingValue,
                                  ValueType = valueType,
                                  Remark = remark,
                                  CreateUser = userId,
                                  CreateTime = TypeConvertHelper.DatetimeToString(DateTime.Now),
                                  IsActive = true
                              };

            var settingRepository = new SettingRepository();
            int insertResult = settingRepository.Create(setting);

            if (insertResult > 0)
                return setting;
            else
                BusinessExceptionHelper.ThrowBusinessException("3000");

            return null;
        }

        public static bool UpdateSetting(int warehouseId, string settingCode, string settingValue, string remark,
                                         int userId)
        {
            Setting setting = GetSetting(warehouseId, settingCode);

            if (setting != null)
            {
                setting.SettingValue = settingValue;
                setting.Remark = remark;
                setting.EditUser = userId;
                setting.EditTime = TypeConvertHelper.DatetimeToString(DateTime.Now);

                var settingRepository = new SettingRepository();
                return settingRepository.Update(setting);
            }

            return false;
        }

        public static bool EnableSetting(int warehouseId, string settingCode)
        {
            Setting setting = GetSetting(warehouseId, settingCode);

            if (setting != null)
            {
                setting.IsActive = true;
                var settingRepository = new SettingRepository();
                return settingRepository.Update(setting);
            }

            return false;
        }

        public static bool DisableSetting(int warehouseId, string settingCode)
        {
            Setting setting = GetSetting(warehouseId, settingCode);

            if (setting != null)
            {
                setting.IsActive = false;
                var settingRepository = new SettingRepository();
                return settingRepository.Update(setting);
            }

            return false;
        }
    }
}